/*
 * LoginFrame.java
 *
 * Created on Apr 20, 2010, 10:58:37 PM
 */

package hirt;

import java.awt.Dialog.ModalExclusionType;

/**
 * LoginFrame acts as the splash screen and entry to the main program. It prompts
 * for a user name. Asks for a password which is not currently in use. Other things,
 * such as advanced options could be added (server address, etc.).
 * @author adieffe
 */
public class LoginFrame extends javax.swing.JFrame
{
    /**
     * Reference to the main hirt client.
     */
    HirtClient client;

    /**
     * Requested player name.
     */
    String playerName = "";

    /**
     * Enter password for this player/user name.
     */
    String password = "";

    /**
     * Constructor that links this object to a client.
     * @param client instance of hirt client.
     */
    public LoginFrame( HirtClient client )
    {
        initComponents();

        this.client = client;
    }

    /**
     * Function that takes the current values of the GUI text fields and
     * sets them accordingly with the rest of the system.
     */
    private void setValues()
    {
        playerName = playerNameTextField.getText().trim();
        password = passwordTextField.getText().trim();

        client.changeName( playerName );
    }

    /**
     * Code to call when "closing" (just hiding) this frame.
     */
    public void close()
    {
        //this.processWindowEvent( new WindowEvent( this, WindowEvent.WINDOW_CLOSING ) );
        this.setModalExclusionType( ModalExclusionType.NO_EXCLUDE );
        this.setVisible( false );
        client.getGUI().setVisible( true );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        playerNameTextField = new javax.swing.JTextField();
        passwordTextField = new javax.swing.JTextField();
        welcomeLabel = new javax.swing.JLabel();
        playerNameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        finishButton = new javax.swing.JButton();
        signInLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        playerNameTextField.setFont(playerNameTextField.getFont().deriveFont(playerNameTextField.getFont().getStyle() & ~java.awt.Font.BOLD, playerNameTextField.getFont().getSize()-1));
        playerNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                playerNameTextFieldActionPerformed(evt);
            }
        });

        passwordTextField.setFont(passwordTextField.getFont().deriveFont(passwordTextField.getFont().getStyle() & ~java.awt.Font.BOLD, passwordTextField.getFont().getSize()-1));
        passwordTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordTextFieldActionPerformed(evt);
            }
        });

        welcomeLabel.setFont(welcomeLabel.getFont().deriveFont(welcomeLabel.getFont().getStyle() & ~java.awt.Font.BOLD, welcomeLabel.getFont().getSize()-1));
        welcomeLabel.setText("Welcome to HIRT.");

        playerNameLabel.setFont(playerNameLabel.getFont().deriveFont(playerNameLabel.getFont().getStyle() & ~java.awt.Font.BOLD, playerNameLabel.getFont().getSize()-1));
        playerNameLabel.setText("Player Name:");

        passwordLabel.setFont(passwordLabel.getFont().deriveFont(passwordLabel.getFont().getStyle() & ~java.awt.Font.BOLD, passwordLabel.getFont().getSize()-1));
        passwordLabel.setText("Password:");

        finishButton.setFont(finishButton.getFont().deriveFont(finishButton.getFont().getStyle() & ~java.awt.Font.BOLD, finishButton.getFont().getSize()-1));
        finishButton.setText("Finish");
        finishButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                finishButtonActionPerformed(evt);
            }
        });

        signInLabel.setFont(signInLabel.getFont().deriveFont(signInLabel.getFont().getStyle() & ~java.awt.Font.BOLD, signInLabel.getFont().getSize()-1));
        signInLabel.setText("Please sign in below:");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/hirt_logo.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(signInLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 480, Short.MAX_VALUE)
                            .addComponent(finishButton, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(welcomeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(playerNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(passwordLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(passwordTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                                    .addComponent(playerNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 364, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(welcomeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(signInLabel)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(playerNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(playerNameTextField))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(finishButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * When finishing entering text into the player name text field, it acts as
     * a close (assumes user is finished).
     * @param evt JTextField ActionEvent.
     */
    private void playerNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playerNameTextFieldActionPerformed
        setValues();
        close();
    }//GEN-LAST:event_playerNameTextFieldActionPerformed

    /**
     * If the user closes the form via the close button in the window decoration,
     * assume that the user wishes to abort the program.
     * @param evt WindowEvent
     */
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        System.exit(7890373);
    }//GEN-LAST:event_formWindowClosing

    /**
     * When finishing entering text into the password text field, it acts as
     * a close (assumes user is finished).
     * @param evt JTextField ActionEvent.
     */
    private void passwordTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordTextFieldActionPerformed
        setValues();
        close();
    }//GEN-LAST:event_passwordTextFieldActionPerformed

    /**
     * When presses the 'Finish' button, it acts as
     * a close (assumes user is finished).
     * @param evt JTextField ActionEvent.
     */
    private void finishButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_finishButtonActionPerformed
        setValues();
        close();
    }//GEN-LAST:event_finishButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton finishButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JTextField passwordTextField;
    private javax.swing.JLabel playerNameLabel;
    private javax.swing.JTextField playerNameTextField;
    private javax.swing.JLabel signInLabel;
    private javax.swing.JLabel welcomeLabel;
    // End of variables declaration//GEN-END:variables

}
